package com.sdry.untils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.basic.entity.ContainerStare;
import com.sdry.basic.entity.Material;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.exception.BizException;
import com.sdry.product.prod.entity.ProductHandleOut;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.mapper.ProductHandleOutMapper;
import com.sdry.product.sorting.entity.SortFrame;
import com.sdry.product.sorting.mapper.SortReceivingDetailMapper;
import com.sdry.product.sorting.mapper.SortReceivingMapper;
import com.sdry.product.sorting.mapper.SortingStockMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Description 自定义转换
 * @Author chenBin
 * @Date 2020-05-06 17:41
 * @Version 1.0
 **/
public class CustomUtil {

    /**
     * 结果转换类，用的地方多
     * @param materialList 输入的结果
     * @return
     */
    public static List<ResultAppList<Material>> QueryResultChange (List<Material> materialList){
        List<ResultAppList<Material>> resultAppListList = new ArrayList<>();
        List<ResultAppList<Material>> newResultAppListList = new ArrayList<>();
        //专门用来存储工位或者容器
        HashSet<String> hashSet = new HashSet<> ();
        //判断集合是否存在
        if (materialList.size() <= 0 || materialList == null) {
            return resultAppListList;
        }
        //遍历存储编号用于判断
        for (Material material : materialList) {
            hashSet.add(material.getPsnumber());
        }
        for (String strNumber : hashSet) {
            ResultAppList resultAppList = new ResultAppList();
            List<Material> materialLists = new ArrayList<>();
            for (Material material : materialList) {
                if (strNumber.equals(material.getPsnumber())) {
                    materialLists.add(material);
                    resultAppList.setPsId(material.getPsId());
                    resultAppList.setNewPsId(material.getPsId());
                    resultAppList.setNewPsnumber(material.getPsnumber());
                    resultAppList.setPsnumber(material.getPsnumber());
                }
            }
            resultAppList.setList(materialLists);
            //将对象放入集合中
            resultAppListList.add(resultAppList);
        }

        for (ResultAppList<Material> materialResultAppList : resultAppListList) {
            if (StringUtils.isNotBlank(materialResultAppList.getPsId())){
                newResultAppListList.add(materialResultAppList);
            }
        }
        return newResultAppListList;
    }

    /**
     * 不判断为psId为空的情况
     * @param materialList
     * @return
     */
    public static List<ResultAppList<Material>> QueryResultChange2 (List<Material> materialList){
        List<ResultAppList<Material>> resultAppListList = new ArrayList<>();
        //专门用来存储工位或者容器
        HashSet<String> hashSet = new HashSet<> ();
        //判断集合是否存在
        if (materialList.size() <= 0 || materialList == null) {
            return resultAppListList;
        }
        //遍历存储编号用于判断
        for (Material material : materialList) {
            hashSet.add(material.getPsnumber());
        }
        for (String strNumber : hashSet) {
            ResultAppList resultAppList = new ResultAppList();
            List<Material> materialLists = new ArrayList<>();
            for (Material material : materialList) {
                if (strNumber.equals(material.getPsnumber())) {
                    materialLists.add(material);
                    resultAppList.setPsId(material.getPsId());
                    resultAppList.setNewPsId(material.getPsId());
                    resultAppList.setNewPsnumber(material.getPsnumber());
                    resultAppList.setPsnumber(material.getPsnumber());
                }
            }
            resultAppList.setList(materialLists);
            //将对象放入集合中
            resultAppListList.add(resultAppList);
        }

        return resultAppListList;
    }


    //验证容器库存
    public static void verifyStockContainer(String psId, SortReceivingMapper sortReceivingMapper) {
        //各区域库存,
        Long total = sortReceivingMapper.queryStockTotal(psId);
        //大于0，说明这个编号存在库存
        if (total > 0){
            throw new BizException(BaseAPIMessageTypeEnum.A097.getName());
        }
    }

    //验证容器库存去除分拣区
    public static void verifyStockContainerExceptFj(String psId, SortReceivingMapper sortReceivingMapper) {
        //各区域库存,
        Long total = 0L;
        //大于0，说明这个编号存在库存
        if (total > 0){
            throw new BizException(BaseAPIMessageTypeEnum.A097.getName());
        }
    }
    //验证容器库存去除前处理区
    public static void verifyStockContainerExceptQcl(String psId, SortReceivingMapper sortReceivingMapper) {
        //各区域库存,
        Long total = 0L;
        //大于0，说明这个编号存在库存
        if (total > 0){
            throw new BizException(BaseAPIMessageTypeEnum.A097.getName());
        }
    }


    /**
     * 预挂线和喷涂线选前处理区物料处理
     * @param material 物料
     * @param stationId 工位ID
     * @param quantity 数量
     * @param productHandleOutMapper
     */
    public static void updatePreTreatment(Material material, Long stationId, Long quantity, ProductHandleOutMapper productHandleOutMapper) {
        //库存数量
        Long stockQuantity = 0L;
        QueryWrapper<ProductHandleOut> queryWrapper02 = new QueryWrapper<>();
        //查询条件
        queryWrapper02.eq("status",2L)
                .eq("station_id",material.getPsId())
                .eq("materials_id",material.getMaId());
        List<ProductHandleOut> productHandleOutList = productHandleOutMapper.selectList(queryWrapper02);
        for (ProductHandleOut productHandleOut : productHandleOutList) {
            stockQuantity += productHandleOut.getQuantity();
        }

        //一个工位显示多行的时候
        for (ProductHandleOut productHandleOut : productHandleOutList) {
            //物料数量
            Long maQuantity = productHandleOut.getQuantity();
            //绑定数量等于库存数量，更改GoSourceId 为值
            if (quantity.equals(stockQuantity)){
                ProductHandleOut productHandleOut01 = new ProductHandleOut();
                //来源的工位ID
                productHandleOut01.setGoSourceId(stationId.toString());
                productHandleOutMapper.update(productHandleOut01,queryWrapper02);
            }else if (quantity > stockQuantity){
                //绑定数量大于库存数量
                throw new BizException(BaseAPIMessageTypeEnum.A096.getName());
            }else if (quantity < stockQuantity){
                //绑定数量小于库存数量,1.绑定数量大于单个物料数量2.绑定数量小于等于单个数量
                ProductHandleOut productHandleOut02 = new ProductHandleOut();
                Long newQuantity = 0L;
                if (quantity > maQuantity){
                    newQuantity = quantity - maQuantity;
                    quantity = newQuantity;
                    productHandleOut02.setQuantity(newQuantity);
                }else if (quantity < maQuantity){
                    newQuantity = maQuantity - quantity;
                    productHandleOut02.setQuantity(newQuantity);
                }else if (quantity.equals(maQuantity)){
                    //来源的工位ID
                    productHandleOut02.setGoSourceId(stationId.toString());
                }
                productHandleOutMapper.update(productHandleOut02,queryWrapper02);
            }
        }
    }

    /**
     * 预挂线和喷涂线选分拣区物料处理
     * @param userId 用户ID
     * @param material 物料
     * @param stationId 工位ID
     * @param sortingStockMapper
     * @param sortReceivingDetailMapper
     */
    public static void updateSortingArea(Long quantity ,String userId, Material material, Long stationId, SortingStockMapper sortingStockMapper, SortReceivingDetailMapper sortReceivingDetailMapper) {
        QueryWrapper<SortFrame> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2L)
                .eq("container_id",material.getPsId());
        //库存查询
        String status = "2";
        ContainerStare containerStare = sortingStockMapper.queryContainerStare(material.getPsId(),material.getMaId(),status);
        Long stockQuantity = Long.valueOf(containerStare.getCsrnum());
        //绑定数量等于库存数量，更改GoSourceId 为值
        if (quantity.equals(stockQuantity)){
            SortFrame sortFrame = new SortFrame();
            //来源的工位ID
            sortFrame.setSource(stationId.toString());
            int update = sortingStockMapper.update(sortFrame, queryWrapper);
            if(update <= 0){
                throw new BizException(BaseAPIMessageTypeEnum.A095.getName());
            }
            //删除装载表信息
            sortReceivingDetailMapper.deleteContainerStareQuantity2(Long.parseLong(material.getPsId()));
        }else if (quantity > stockQuantity){
            //绑定数量大于库存数量
            throw new BizException(BaseAPIMessageTypeEnum.A096.getName());
        }else if (quantity < stockQuantity){
            //绑定数量小于库存数量
            Long newQuantity = stockQuantity - quantity;
            Integer effect = sortReceivingDetailMapper.updateContainerStareQuantity3(material.getPsId(), material.getMaId(), newQuantity, userId);
            if(effect <= 0){
                throw new BizException(BaseAPIMessageTypeEnum.A095.getName());
            }
        }
    }
}
